package com.spzx.product;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.spzx.common.security.annotation.EnableCustomConfig;
import com.spzx.common.security.annotation.EnableRyFeignClients;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.service.ProductSkuService;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.stream.Collectors;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
@EnableKnife4j
public class SpzxProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpzxProductApplication.class,args);
        System.out.println("成功");



    }

    @Resource
    RedissonClient redissonClient;
    @Resource
    ProductSkuService productSkuService;

    @PostConstruct
    public void initSkuBloomfilter(){
        //1、查询所有skuid集合
        List<Long> skuIdList = productSkuService.list(new LambdaQueryWrapper<ProductSku>().select(ProductSku::getId))
                .stream().map(ProductSku::getId).collect(Collectors.toList());

        //创建一个布隆过滤器
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter("bloom:filter:spzx:skuIds");
        //布隆过滤器初始化 长度为skuid的个数
        bloomFilter.tryInit(skuIdList.size(),0.01);

        //将skuIdlist存到布隆过滤器中
        skuIdList.forEach(skuId->{

            bloomFilter.add(skuIdList);
        });



    }

}